﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brouteforce.Utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Brouteforce.NewManagers {
    class InputManager : Manager {

        public static InputManager Instance = new InputManager();


        private static Keys[] _lastPressedKeys = new Keys[0];
        private bool _mouseWasPressed = false;
        public MonitoredProperty<bool> MouseClicked = new MonitoredProperty<bool>(false);
        public int CurMouseX, CurMouseY;

        /// <summary>
        /// return list of all the keyboard keys that was clicked (= was down and now released) in this update
        /// </summary>
        public static List<Keys> CurrentClickedKeys = new List<Keys>();

        public static bool IsShiftDown = false;


        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, BFGame game) {
        }

        protected override void _Update(Microsoft.Xna.Framework.GameTime gameTime, BFGame game) {
            UpdateMouse();
            UpdateKeyBoard();
        }

        private void UpdateMouse() {
            MouseState ms = Mouse.GetState();

            ms = Mouse.GetState();

            CurMouseX = ms.X;
            CurMouseY = ms.Y;

            if (!BFGame.Graphics.GraphicsDevice.Viewport.Bounds.Contains(ms.X, ms.Y))
                return;

            if (ms.LeftButton == ButtonState.Pressed) {
                if (!_mouseWasPressed) {
                    _mouseWasPressed = true;
                }
            } else if (ms.LeftButton == ButtonState.Released) {
                if (_mouseWasPressed) {
                    _mouseWasPressed = false;
                    MouseClicked.Property = true;
                }
            }
        }


        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content) {
        }

        public override void Initialize() {
            BFGame.Instance.IsMouseVisible = true;
        }


        public bool IsMouseClickedOn(Rectangle zone) {
            return MouseClicked.Property && zone.Contains(CurMouseX, CurMouseY);
        }

        
        private static void UpdateKeyBoard() {
            CurrentClickedKeys.Clear();
            KeyboardState kbState = Keyboard.GetState();
            Keys[] pressedKeys = kbState.GetPressedKeys();

            //check if any of the previous update's keys are no longer pressed
            foreach (Keys key in _lastPressedKeys) {
                if (!pressedKeys.Contains(key)) {
                    OnKeyUp(key);
                    CurrentClickedKeys.Add(key);
                }
            }

            //save the currently pressed keys so we can compare on the next update
            _lastPressedKeys = pressedKeys;
            IsShiftDown = kbState.IsKeyDown(Keys.LeftShift) || kbState.IsKeyDown(Keys.RightShift);
        }


        private static void OnKeyUp(Keys key) {
            //do stuff
        }
    }
}
